/**
 * All rights Reserved, Designed By 信鸥科技
 * project : committee
 * Created by youquer on 17/10/25.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */

import { postList, postInfo, postUpdate, postAdd, postDelete } from '@/services/powers/app';
import { message } from 'antd';
import queryString from 'query-string';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'app',

  state: {
    type: "app",
    id: '', //id
    //分页
    page: 1, //当前页
    pageSize: 10, //每页条数
    total: 0, //总页数
    //列表页-添加页提交按钮loading
    loading: true,//添加页确定按钮loading
    dataList: [], //列表页数据数据
    isUpdate: 0, //是否是修改页: 0:添加页, 1:修改页

    //搜索部分
    //搜索字段
    values:"",
    advanceTails:{}, //高级搜索内容
    searchValue:"", //普通搜索内容
    selectValue:"", //普通搜索
    isAdvance:false, //高级搜索暂开
    expand: false, //是否有高级搜索
    searchNo: true, //是否存在搜索-不存在搜索
    commonSelectData:[], //普通搜索内容,

    //tabs标签
    key: '1' //1: 可用:标签一, 2:不可用:标签二

  },

  reducers: {

    /**
     * 赋值数据
     * */
    setState(state, action){
      return {...state, ...action.payload};
    },

    //分页act
    changePage(state, action){
      return {...state, page: action.page, pageSize: action.pageSize}
    },

    /**
     * 列表页接口-列表页函数
     *
     * */
    //搜索
    searchData(state,action){
      if (action.key != undefined){
        state.advanceTails[action.key] = action.value;
      }

      if (action.selectValue != undefined){
        state.selectValue = action.selectValue;
      }

      //当点击搜索的时候调起接口
      if (action.searchValue != undefined){
        state.searchValue = action.searchValue;
      }
      return{...state,expand:action.expand};
    },

    //搜索重置
    advanceReset(state,action){
      return {...state,...action.payload,loading: false}
    },

    /**
     * 添加-修改页函数
     * */
    //添加成功
    addSuccess(state, action){
      return {...state,...action.payload, loading: false};
    },

    //选择姓名回调
    clickNameSuccess(state, action){
      return{...state, ...action.payload};
    },

    //添加页获取数据
    changeState(state, action){
      state[action.key] = action.value;
      return {...state, state};
    },

    //提交按钮
    submit(state,action){
      return {...state,loading:action.loading}
    },
    //提交成功
    querySuccess(state, action){

      return {...state,...action.payload, loading: false};
    },
    clearData(state){
      return {...state,
        code: '',
        name: ''
      };
    }
  },

  subscriptions:{
    setup({ dispatch, history }){
      history.listen(location => {
        if (location.pathname === '/permissions/appEdit'){
          var search = queryString.parse(location.search);
          dispatch({
            type: "setState",
            payload: {isUpdate: 1, id: search.id}
          });
          dispatch({
            type: "getInfo",
            payload: {id: search.id}
          });
          dispatch({
            type: 'clearData'
          })
        } else if (location.pathname === '/permissions/appAdd') {
          dispatch({
            type: "setState",
            payload: {isUpdate: 0}
          });
          dispatch({
            type: 'clearData'
          });
        }
      })
    }
  },

  effects:{
    //列表页,分页,搜索页
    *dataList({payload}, { call, put ,select}) {
      var dates = yield select(state => state.app);
      var params = {};
      params["pageSize"] = dates.pageSize;
      params["selectValue"] = dates.selectValue;
      params["searchValue"] = dates.searchValue;
      if (payload){
        params["page"] = payload;
      } else {
        params["page"] = dates.page;
      }
      const data = yield call(postList, params);
      if (data) {
        // 但是现在 我只能获取到, 纯粹的 rest 返回结果 是个数组. 我尝试在 request 类中重新封装数据, 各种尝试都失败了. 各种搜  可能是关键词不对  也没有找到解决办法.
        if (data.recode == 200){
          yield put({
            type: 'setState',
            payload: {
              dataList: data.body.dataList,
              total: data.body.total,
              loading:false
            }
          });
        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          yield put(routerRedux.push("/user/login"));
        } else {
          message.error("网络连接失败");
          yield  put({
            type: "setState",
            payload: {loading: false}
          })
        }
      }
    },
    //获取详情信息
    *getInfo({payload}, { call, put }){
      const data = yield call(postInfo, payload);
      if (data){
        if (data.recode == 200){
          yield put({
            type: 'setState',
            payload: data.body.app,
          })
        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          yield put(routerRedux.push("/user/login"));
        } else {
          message.error('网络请求失败!');
        }
      } else {
        message.error("网络请求失败");
      }
    },
    //删除
    *delete({payload}, {call, put}){
      const dic = {id: payload};
      const data = yield call(postDelete,dic);
      if (data){
        if (data.recode == 200){
          message.success("删除该条信息成功");
          yield put({
            type: 'dataList',
          });
        } else if (data.recode == 9000){
          message.error("您的权限不足");
        } else if (data.recode == 1001){
          yield put(routerRedux.push("/user/login"));
        } else {
          message.error('网络请求失败!');
        }
      } else {
        message.error("网络请求失败!>_<");
      }
    },
    //添加修改网络请求
    *update({payload}, {call, put, select}) {
      if (payload.code && payload.name){
        if (payload.isEnable){
          payload.isEnable = 1;
        } else {
          payload.isEnable = 0;
        }
        payload.id = yield select(state => state.app.id);
        const data = yield call(postUpdate,{...payload});
        if (data) {
          if (data.recode == 200){
            message.success('提交成功');
            yield put({
              type:'addSuccess',
              payload:{
                loading: false,
              }
            });
            yield put(routerRedux.push("/permissions/app"));

          } else if (data.recode == 9000){
            message.error("您的权限不足");
          } else if (data.recode == 1001){
            yield put(routerRedux.push("/user/login"));
          } else {
            message.error('网络请求失败,请重新提交');
            yield put({
              type:'submit',
              loading: false
            })
          }
        } else {
          yield put({
            type:'submit',
            loading: false
          })
        }
      }else {
        message.warn('所有信息均为必填信息');
        yield put({
          type:'submit',
          loading: false
        })
      }
    },
    //添加接口
    *add({payload}, {call, put, select}) {
      if (payload.code && payload.name){
        const data = yield call(postAdd,{...payload});
        if (data) {
          if (data.recode == 200){
            message.success('提交成功');
            yield put({
              type:'addSuccess',
              payload:{
                loading: false,
              }
            });
            yield put(routerRedux.push("/permissions/app"));
          } else if (data.recode == 9000){
            message.error("您的权限不足");
          } else if (data.recode == 1001){
            yield put(routerRedux.push("/user/login"));
          } else {
            message.error('网络请求失败,请重新提交');
            yield put({
              type:'submit',
              loading: false
            })
          }
        } else {
          yield put({
            type:'submit',
            loading: false
          })
        }
      }else {
        message.warn('所有信息均为必填信息');
        yield put({
          type:'submit',
          loading: false
        })
      }
    },
  },
}
